package kr.hyunist.androidcomplete01saeildamil.ch12;

import kr.hyunist.androidcomplete01saeildamil.R;
import kr.hyunist.androidcomplete01saeildamil.ch12.DAO.MentalTestDAO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MentalTestEx extends Activity {
	RadioGroup mRadiogroup;
	Button resultBtn;
	Button prevBtn;
	Button nextBtn;
	TextView titleTv;
	TextView resultTv;
	RadioButton radio1;
	RadioButton radio2;
	RadioButton radio3;
	RadioButton radio4;
	int mIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mentaltest);

		refresh();

		mRadiogroup = (RadioGroup) findViewById(R.id.meantaltestradiogroup);
		// mRadiogroup.setOnCheckedChangeListener(mRdoChk);

		resultBtn = (Button) findViewById(R.id.mentaltestresultbtnzzz);
		resultBtn.setOnClickListener(mOnClickListener);

		prevBtn = (Button) findViewById(R.id.mentaltestprevbtn);
		nextBtn = (Button) findViewById(R.id.mentaltestnextbtn);

		prevBtn.setOnClickListener(mOnClickListener2);
		nextBtn.setOnClickListener(mOnClickListener2);

		resultTv = (TextView) findViewById(R.id.mentaltestresulttvzzz);

	}

	OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			resultTv.setVisibility(View.VISIBLE);
			switch (mRadiogroup.getCheckedRadioButtonId()) {
			case R.id.mentaltestrdo1:
				resultTv.setText(MentalTestDAO.resultArray[mIndex][0]);
				break;
			case R.id.mentaltestrdo2:
				resultTv.setText(MentalTestDAO.resultArray[mIndex][1]);
				break;
			case R.id.mentaltestrdo3:
				resultTv.setText(MentalTestDAO.resultArray[mIndex][2]);
				break;
			case R.id.mentaltestrdo4:
				resultTv.setText(MentalTestDAO.resultArray[mIndex][3]);
				break;
			}
		}

	};

	OnClickListener mOnClickListener2 = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mentaltestprevbtn:

				if (mIndex == 0) {
					mIndex = 2;
				} else {
					mIndex--;
				}
				resultTv.setText("");
				mRadiogroup.clearCheck();
				refresh();
				break;

			case R.id.mentaltestnextbtn:

				if (mIndex == MentalTestDAO.sQuestion.length - 1) {
					mIndex = 0;
				} else {
					mIndex++;
				}
				resultTv.setText("");
				mRadiogroup.clearCheck();
				refresh();
				break;
			}
		}

	};

	public void refresh() {
		titleTv = (TextView) findViewById(R.id.mentaltesttitle);
		titleTv.setText(MentalTestDAO.sQuestion[mIndex]);

		radio1 = (RadioButton) findViewById(R.id.mentaltestrdo1);
		radio2 = (RadioButton) findViewById(R.id.mentaltestrdo2);
		radio3 = (RadioButton) findViewById(R.id.mentaltestrdo3);
		radio4 = (RadioButton) findViewById(R.id.mentaltestrdo4);

		radio1.setText(MentalTestDAO.radioArray[mIndex][0]);
		radio2.setText(MentalTestDAO.radioArray[mIndex][1]);
		radio3.setText(MentalTestDAO.radioArray[mIndex][2]);
		radio4.setText(MentalTestDAO.radioArray[mIndex][3]);
	}

	/*
	 * OnCheckedChangeListener mRdoChk = new
	 * RadioGroup.OnCheckedChangeListener() {
	 * 
	 * @Override public void onCheckedChanged(RadioGroup group, int checkedId) {
	 * if (group.getId() == R.id.meantaltestradiogroup) {
	 * 
	 * } }
	 * 
	 * };
	 */
}
